﻿using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WorkOne.Webapi.Extention;
using WorkOne.Domain;
using CSRedis;
using WorkOne.Domain.Model;

namespace WorkOne.Webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PayController : ControllerBase
    {
        private readonly GoodsDbContext db;

        public PayController(GoodsDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 支付宝支付
        /// </summary>
        /// <param name="OrderName"></param>
        /// <param name="money"></param>
        /// <returns></returns>
        [HttpGet("BtnAlipay_Click")]
        public IActionResult BtnAlipay_Click(string? OrderName, decimal money)
        {
            DefaultAopClient client = new DefaultAopClient(config.gatewayUrl, config.app_id, config.private_key, "json", "1.0", config.sign_type, config.alipay_public_key, config.charset, false);

            // 外部订单号，商户网站订单系统中唯一的订单号
            string out_trade_no = Guid.NewGuid().ToString();

            // 订单名称
            string subject = "苹果promax";

            // 付款金额
            string total_amount = money.ToString();

            // 商品描述
            string body = "很看好机会";

            // 组装业务参数model
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.Body = body;
            model.Subject = subject;
            model.TotalAmount = total_amount;
            model.OutTradeNo = out_trade_no;
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";

            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 设置同步回调地址
            request.SetReturnUrl("http://localhost:8080/about");
            // 设置异步通知接收地址
            request.SetNotifyUrl("http://ey53gb.natappfree.cc/api/Shop/Update");
            // 将业务model载入到request
            request.SetBizModel(model);

            AlipayTradePagePayResponse response = null;
            string res = "";
            try
            {
                response = client.pageExecute(request, null, "post");
                res = response.Body;
            }
            catch (Exception exp)
            {
                throw exp;
            }

            return Ok(res);
        }
    }
}